<?php

/*
	This file is part of Mandragon.

    Mandragon is free software: you can redistribute it and/or modify
    it under the terms of the GNU General Public License as published by
    the Free Software Foundation, either version 3 of the License, or
    (at your option) any later version.

    Mandragon is distributed in the hope that it will be useful,
    but WITHOUT ANY WARRANTY; without even the implied warranty of
    MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
    GNU General Public License for more details.

    You should have received a copy of the GNU General Public License
    along with Mandragon.  If not, see <http://www.gnu.org/licenses/>.
*/

require_once 'PHPUnit/Framework.php';
require_once 'autoload.php';

class TestActivatePage extends PHPUnit_Framework_TestCase {
	
	function test_page_inc_check_access() {

		$page = new Page();

		$activate_page = new ActivatePage();

		$mock_access = $this->getMock('AccessManager');
		$mock_access->expects($this->once())
				->method('check_access')
				->with('VIEW');
		$activate_page->set_access_manager($mock_access);

		$mock_toolbox = $this->getMock('Toolbox');
		$mock_toolbox->expects($this->never())
				->method('endpage');
		$activate_page->set_toolbox($mock_toolbox);
		$activate_page->page_inc_check_access($page);
	}
	
	function test_page_inc_check_access_with_user_id() {

		$page = new Page();

		$activate_page = new ActivatePage();

		$_SESSION['user_id'] = true;

		$mock_access = $this->getMock('AccessManager');
		$mock_access->expects($this->once())
				->method('check_access')
				->with('VIEW');
		$activate_page->set_access_manager($mock_access);

		$mock_toolbox = $this->getMock('Toolbox');
		$mock_toolbox->expects($this->once())
				->method('endpage')
				->with($page, 'already registered');
		$activate_page->set_toolbox($mock_toolbox);
		$activate_page->page_inc_check_access($page);
	}

	function test_page_inc_execute_action() {
		$expected_action = 'some action';
		
		$page = new Page();

		$activate_page = new ActivatePage();

		$mock_alerter = $this->getMock('Alerter');
		$mock_alerter->expects($this->once())
				->method('add_alert')
				->with("unknown action: $expected_action");
		$activate_page->set_alerter($mock_alerter);

		$activate_page->page_inc_execute_action($expected_action);
	}

	function test_page_inc_execute_with_no_code() {
		$page = new Page();

		$activate_page = new ActivatePage();
		$exception = new Exception();

		$mock_error_nav = $this->getMock('ErrorNavigation');
		$mock_error_nav->expects($this->once())
				->method('errorpage')
				->with(9)
				->will($this->throwException($exception));
		$activate_page->set_error_navigation($mock_error_nav);

		try {
			$activate_page->page_inc_execute($page);
		} catch (Exception $nav_exception) {
			//Ignore
		}
	}

	function test_page_inc_execute_with_code() {
		$expected_get_code = 12345;
		$expected_user_id = "23456";
		$expected_select_query = 'select query';
		$expected_sql_result = 'sql result';
		$expected_num_rows = 0;
		$expected_exception = new Exception();

		$page = new Page();
		$page->id = $expected_user_id;

		$_GET['code'] = $expected_get_code;

		$activate_page = new ActivatePage();

		$mock_db_access = $this->getMock('DbAccessor');
		$mock_db_access->expects($this->at(0))
				->method('db_query_select')
				->with(array(
								array('*')), 
						array("MEMBER"), 
						"user_id = $expected_user_id")
				->will($this->returnValue($expected_select_query));
		$mock_db_access->expects($this->at(1))
				->method('db_do_query')
				->with($expected_select_query)
				->will($this->returnValue($expected_sql_result));
		$mock_db_access->expects($this->at(2))
				->method('db_num_rows')
				->with($expected_sql_result)
				->will($this->returnValue($expected_num_rows));

		$activate_page->set_db_accessor($mock_db_access);

		$mock_error_nav = $this->getMock('ErrorNavigation');
		$mock_error_nav->expects($this->once())
				->method('errorpage')
				->with(9)
				->will($this->throwException($expected_exception));
		$activate_page->set_error_navigation($mock_error_nav);

		try {
			$activate_page->page_inc_execute($page);
		} catch (Exception $nav_exception) {
			//Ignore
		}
	}

	function test_page_inc_execute_with_incorrect_code() {

		$expected_get_code = 12345;
		$expected_user_id = "23456";
		$expected_select_query = 'select query';
		$expected_sql_result = 'sql result';
		$expected_num_rows = 1;
		$expected_exception = new Exception();
		$expected_user = array();

		$page = new Page();
		$page->id = $expected_user_id;

		$_GET['code'] = $expected_get_code;

		$activate_page = new ActivatePage();

		$mock_db_access = $this->getMock('DbAccessor');
		$mock_db_access->expects($this->at(0))
				->method('db_query_select')
				->with(array(
								array('*')), 
						array("MEMBER"), 
						"user_id = $expected_user_id")
				->will($this->returnValue($expected_select_query));
		$mock_db_access->expects($this->at(1))
				->method('db_do_query')
				->with($expected_select_query)
				->will($this->returnValue($expected_sql_result));
		$mock_db_access->expects($this->at(2))
				->method('db_num_rows')
				->with($expected_sql_result)
				->will($this->returnValue($expected_num_rows));
		$mock_db_access->expects($this->at(3))
				->method('db_fetch_array')
				->with($expected_sql_result)
				->will($this->returnValue($expected_user));

		$activate_page->set_db_accessor($mock_db_access);

		$mock_error_nav = $this->getMock('ErrorNavigation');
		$mock_error_nav->expects($this->once())
				->method('errorpage')
				->with(12)
				->will($this->throwException($expected_exception));
		$activate_page->set_error_navigation($mock_error_nav);

		try {
			$activate_page->page_inc_execute($page);
		} catch (Exception $nav_exception) {
			//Ignore
		}
	}
}


?>
